<script lang="ts" setup>
import { getListByConditions, insertOne, urls, physicalDeleteById, updateOne } from '@/index';
import type { DrawerModel, NdeReportNdeDefect, NdeReportRejectPart } from '@/index.d';
import { useRequest } from 'vue-request';
import { personList, defectTypeList } from './optionList';

const drawerModel = defineModel<DrawerModel<NdeReportRejectPart>>('drawerModel', {
  required: true
});

const title = computed(() => {
  const data = drawerModel.value.data;
  if (data) {
    return (
      '报告：' +
      data.reportReference +
      '，工件：' +
      data.partName +
      '（' +
      data.partReference +
      '）'
    );
  } else {
    return '获取数据错误';
  }
});

const {
  data: getNdeDefectListData,
  run: getNdeDefectListRun,
  loading: getNdeDefectListLoading
} = useRequest(() =>
  getListByConditions<NdeReportNdeDefect>(urls.ndeReportNdeDefect, {
    rejectPartId: drawerModel.value.id
  })
);

watch(
  () => drawerModel.value.visible,
  (value: boolean) => {
    value && getNdeDefectListRun();
  }
);

const insertNdeDefect = async () => {
  const defect: NdeReportNdeDefect = {
    isAcceptable: false,
    rejectPartId: drawerModel.value.data?.id,
    reportReference: drawerModel.value.data?.reportReference,
    partReference: drawerModel.value.data?.partReference
  };
  const res = await insertOne<NdeReportNdeDefect>(urls.ndeReportNdeDefect, defect);
  if (res) {
    getNdeDefectListRun();
  }
};

const handleDelete = (id: number) => {
  physicalDeleteById(urls.ndeReportNdeDefect, id).then(() => {
    ElMessage('删除成功' + id);
    getNdeDefectListRun();
  });
};

const updateCurrentDefectTesterName = (id: number, testerName: string) => {
  const ndeDefect: NdeReportNdeDefect = {
    id,
    testerName
  };
  updateOne<NdeReportNdeDefect>(urls.ndeReportNdeDefect, ndeDefect).then(() => {
    ElMessage('修改成功');
    getNdeDefectListRun();
  });
};

const updateCurrentDefectType = (id: number, defectType: string) => {
  const ndeDefect: NdeReportNdeDefect = {
    id,
    defectType
  };
  updateOne<NdeReportNdeDefect>(urls.ndeReportNdeDefect, ndeDefect).then(() => {
    ElMessage('修改成功');
    getNdeDefectListRun();
  });
};

const updateCurrentDefectSize = (id: number, defectSize: number) => {
  const ndeDefect: NdeReportNdeDefect = {
    id,
    defectSize
  };
  updateOne<NdeReportNdeDefect>(urls.ndeReportNdeDefect, ndeDefect).then(() => {
    ElMessage('修改成功');
    getNdeDefectListRun();
  });
};

const updateCurrentDefectTestResult = (id: number, isAcceptable: boolean) => {
  const ndeDefect: NdeReportNdeDefect = {
    id,
    isAcceptable
  };
  updateOne<NdeReportNdeDefect>(urls.ndeReportNdeDefect, ndeDefect).then(() => {
    ElMessage('修改成功');
    getNdeDefectListRun();
  });
};
</script>
<template>
  <el-drawer v-model="drawerModel.visible" :title destroy-on-close size="50%">
    <template #default>
      <el-button type="primary" @click="insertNdeDefect">新增缺陷</el-button>
      <br />
      <br />
      <HqTable
        :base-url="urls.ndeReportNdeDefect"
        title="无损操作记录"
        :table-data="getNdeDefectListData"
        :loading="getNdeDefectListLoading"
        :on-handle-reload="getNdeDefectListRun"
      >
        <template #columns>
          <HqTableColumn label="检测人员">
            <template #default="{ row }">
              <el-select
                v-model="row.testerName"
                @change="updateCurrentDefectTesterName(row.id, row.testerName)"
              >
                <el-option v-for="item in personList" :key="item" :label="item" :value="item" />
              </el-select>
            </template>
          </HqTableColumn>
          <HqTableColumn label="缺陷类型">
            <template #default="{ row }">
              <el-select
                v-model="row.defectType"
                @change="updateCurrentDefectType(row.id, row.defectType)"
              >
                <el-option
                  v-for="item in defectTypeList[drawerModel.data?.testMethod as string]"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </template>
          </HqTableColumn>
          <HqTableColumn label="缺陷尺寸">
            <template #default="{ row }">
              <el-input
                v-model="row.defectSize"
                @blur="updateCurrentDefectSize(row.id, row.defectSize)"
              />
            </template>
          </HqTableColumn>
          <HqTableColumn label="是否合格">
            <template #default="{ row }">
              <el-switch
                v-model="row.isAcceptable"
                @change="updateCurrentDefectTestResult(row.id, row.isAcceptable)"
              />
            </template>
          </HqTableColumn>
          <HqTableColumn label="删除">
            <template #default="{ row }">
              <el-popconfirm title="确认删除？" @confirm="handleDelete(row.id)">
                <template #reference>
                  <el-button type="danger" text>删除</el-button>
                </template>
              </el-popconfirm>
            </template>
          </HqTableColumn>
        </template>
      </HqTable>
    </template>
  </el-drawer>
</template>
<style lang="scss" scoped></style>
